



#import "YTKNetHandle.h"
#import "MBProgressHUD.h"
#import "UIColor+YYAdd.h"

@interface YTKNetHandle()


@end


@implementation YTKNetHandle


{
    NSDictionary *_header;
    NSString *_url;
    NSDictionary *_paramDic;
    RegisterApiMethod _registerApiMethod;
    MBProgressHUD *hud;

}



- (id)initWithUrl:(NSString *)url header:(NSDictionary *)header RegisterApiMethod:(RegisterApiMethod)registerApiMethod
{
    self = [super init];
    if (self) {
        _url = url;
        _header = header;
        _registerApiMethod = registerApiMethod;
    }
    return self;

}
- (id)initWithUrl:(NSString *)url paramDic:(NSDictionary *)paramDic RegisterApiMethod:(RegisterApiMethod)registerApiMethod
{
    self = [super init];
    if (self) {
        _url = url;
        _paramDic = paramDic;
        _registerApiMethod = registerApiMethod;
    }
    return self;
}

- (id)initWithUrl:(NSString *)url RegisterApiMethod:(RegisterApiMethod)registerApiMethod
{
    self = [super init];
    if (self) {
        _url = url;
        _registerApiMethod = registerApiMethod;
    }
    return self;

}




- (NSString *)requestUrl {
    
    // “http://www.yuantiku.com” 在 YTKNetworkConfig 中设置，这里只填除去域名剩余的网址信息
    return _url;
}
- (id)requestArgument {
    return _paramDic;
}

- (AFConstructingBlock)constructingBodyBlock
{

    
    return self.responseBody;
}
- (YTKRequestMethod)requestMethod {
    
    
    switch (_registerApiMethod) {
        case 0:
            return  YTKRequestMethodGet;
            break;
        case 1:
            return YTKRequestMethodPost;
            break;
        case 2:
            return YTKRequestMethodHead;
            break;
        case 3:
            return YTKRequestMethodPut;
            break;
        case 4:
            return YTKRequestMethodDelete;
            break;
        default:
            return YTKRequestMethodPatch;
            break;
    }
}



- (NSDictionary *)requestHeaderFieldValueDictionary
{
    return _header;
}

- (void)requestFailedFilter
{
    [hud hide:YES];
    if (self.responseStatusCode == 401) {
        // 401登录异常
        [[NSNotificationCenter defaultCenter] postNotificationName:@"tokenError" object:nil];
    }else{
        // 网络连接错误
        [[NSNotificationCenter defaultCenter] postNotificationName:@"netError" object:nil userInfo:@{@"msg":@"网络连接失败"}];

    }
    
}
- (NSTimeInterval)requestTimeoutInterval
{
    // 超时设置
    return 5;
}

- (void)requestCompleteFilter
{

    [hud hide:YES];
    if ([self.responseBody objectForKey:@"error"] != 0) {
        if (self.responseBody != nil) {
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"netError" object:nil userInfo:@{@"msg":self.responseBody[@"msg"]}];
        }

    }else{
        
        NSLog(@"%@", self.responseBody[@"msg"]);
    }

}

- (void)startWithCompletionBlockWithSuccess:(void (^)(YTKBaseRequest *))success failure:(void (^)(YTKBaseRequest *))failure
{
    if (hud == nil) {
        UIWindow *window = [[UIApplication sharedApplication].windows lastObject];

        hud = [[MBProgressHUD alloc] initWithWindow:window];
        hud.removeFromSuperViewOnHide = YES;
        
            [window addSubview:hud];
        hud.labelText = @"数据正在加载中...";
        hud.labelColor = [UIColor colorWithHexString:@"#A1A1AC"];
        hud.labelFont = [UIFont systemFontOfSize:15];
        hud.mode = MBProgressHUDModeCustomView;
        hud.progress = 0;
//        YYImage *img = [YYImage imageNamed:@"loadingGIF"];
//        YYAnimatedImageView *imgV = [[YYAnimatedImageView alloc] initWithImage:img];
//        imgV.height = 80;
//        imgV.width = 80;
//        hud.customView = imgV;
//        hud.progress = 0;
//        hud.color = [UIColor clearColor];
        [hud show:YES];
        
    }
   
    [super startWithCompletionBlockWithSuccess:success failure:failure];
   



}

    
    
    @end